using System;
using System.Collections.Generic;
using System.Text;

using Db4objects.Db4o;
using SagaLib;
using SagaDB;

namespace DB4OServer
{
    class Program
    {
        static void Main(string[] args)
        {
            DBServerConfig lcfg = new DBServerConfig();
            WorldConfig wcfg = new WorldConfig();
            List<IObjectServer> chardbs = new List<IObjectServer>();
            
            Db4oFactory.Configure().GenerateVersionNumbers(System.Int32.MaxValue);
            Db4oFactory.Configure().GenerateUUIDs(System.Int32.MaxValue);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.User)).GenerateVersionNumbers(false);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.User)).GenerateUUIDs(false);

            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.User)).CascadeOnActivate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.User)).CascadeOnUpdate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.User)).CascadeOnDelete(true);

            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorPC)).CascadeOnActivate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorPC)).CascadeOnUpdate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorPC)).CascadeOnDelete(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorPC)).StoreTransientFields(false);

            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorNPC)).CascadeOnActivate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorNPC)).CascadeOnUpdate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorNPC)).CascadeOnDelete(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Actors.ActorNPC)).StoreTransientFields(false);

            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Items.Inventory)).CascadeOnActivate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Items.Inventory)).CascadeOnUpdate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Items.Inventory)).CascadeOnDelete(true);

            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Items.Item)).CascadeOnActivate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Items.Item)).CascadeOnUpdate(true);
            Db4oFactory.Configure().ObjectClass(typeof(SagaDB.Items.Item)).CascadeOnDelete(true);


            IObjectServer userdb;
            try
            {
                userdb = Db4oFactory.OpenServer(lcfg.UserDbFile, lcfg.DBPort);
                userdb.GrantAccess(lcfg.DBUser, lcfg.DBPass);

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: Cannot start UserDB: " + e.ToString());
                Console.WriteLine("Shutting down in 50sec.");
                System.Threading.Thread.Sleep(50000);
                return;
            }
            Console.WriteLine("User database has been started.");

            foreach (WorldConfig.World world in wcfg.Worlds.Values)
            {
                string file = world.Name + ".db";
                IObjectServer newdb;
                try
                {
                    newdb = Db4oFactory.OpenServer(file, world.DBPort);
                    newdb.GrantAccess(world.DBUser, world.DBPass);
                }
                catch (Exception)
                {
                    Console.WriteLine("Error: Cannot database for world: "+world.Name);
                    Console.WriteLine("Shutting down in 20sec.");
                    System.Threading.Thread.Sleep(20000);
                    return;
                }
                chardbs.Add(newdb);
                Console.WriteLine("Char database for world: "+world.Name +" has been started.");
            }

            Console.WriteLine("Saga DB4O Server started");
            Console.WriteLine("Enter 'quit' to exit...");
            while (true)
            {
                string line = Console.ReadLine().ToLower();
                if (line == "quit")
                    break;
            }
            userdb.Close();
            foreach (IObjectServer tdb in chardbs)
                tdb.Close();
            Console.WriteLine("Saga DB4O Server killed.");
        }
    }
}
